﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Net;

namespace BattleShips.GameClients {
    public partial class WelcomeScreen : Form {
        public WelcomeScreen() {
            InitializeComponent();
        }

        public Action<string, int> ConnectToServer { get; set; }
        public Action<string, string> LoginAction { get; set; }
        public Action<string, string> RegisterAction { get; set; }

        private void btnLogin_Click(object sender, EventArgs e) {
            if (txtLoginUsername.Text != String.Empty && txtLoginPassword.Text != String.Empty) {
                try {
                    if (LoginAction != null)
                        LoginAction(txtLoginUsername.Text, txtLoginPassword.Text);
                } catch (Exception ex) {
                    lblLoginError.Visible = true;
                    lblLoginError.Text = ex.Message;
                }
            } else {
                lblLoginError.Visible = true;
                lblLoginError.Text = "Toate campuri trebuiesc completate !";
            }
        }
        
        private void lblRegister_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            pnlLogin.Visible = false;
            pnlRegister.Visible = true;
        }

        private void lblLogin_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e) {
            pnlLogin.Visible = true;
            pnlRegister.Visible = false;
        }

        private void btnRegister_Click(object sender, EventArgs e) {
            if (txtRegisterUsername.Text != String.Empty && txtRegisterPassword.Text != String.Empty) {
                try {
                    if (RegisterAction != null)
                        RegisterAction(txtRegisterUsername.Text, txtRegisterPassword.Text);
                    pnlRegister.Visible = false;
                    pnlLogin.Visible = true;
                    pnlLogin.Enabled = true;
                } catch (Exception ex) {
                    lblRegisterError.Visible = true;
                    lblRegisterError.Text = ex.Message;
                }
            } else {
                lblRegisterError.Visible = true;
                lblRegisterError.Text = "Toate campuri trebuiesc completate !";
            }
        }
        private bool connected = false;
        private void btnConnect_Click(object sender, EventArgs e) {
            int port = 0;
            Int32.TryParse(txtServerPort.Text, out port);
            try {
                if (ConnectToServer != null)
                    ConnectToServer(txtServerIP.Text, port);
                lblConnectionError.Visible = true;
                lblConnectionError.Text = "Successful connected to " + txtServerIP.Text + " .";
                btnConnect.Enabled = false;
                txtServerIP.Enabled = false;
                txtServerPort.Enabled = false;
                pnlLogin.Enabled = true;
                pnlRegister.Enabled = true;
                connected = true;
            } catch (Exception) {
                lblConnectionError.Visible = true;
                lblConnectionError.Text = "No server found . Please try again !";
            }
        }

        private void WelcomeScreen_FormClosed(object sender, FormClosedEventArgs e) {
            if (connected)
                this.Close();
            else
                Environment.Exit(0);
        }
    }
}
